package com.kzd.gtboon.jetpack.api

import com.kzd.gtboon.app.*
import com.kzd.gtboon.entity.*
import com.kzd.gtboon.utils.httpSign
import com.kzd.gtboon.utils.key1
import com.module.lemlin.http.HttpResponseDataE
import com.module.lemlin.http.HttpResponseDataT
import rxhttp.RxHttp
import rxhttp.toClass
import rxhttp.toDownload
import rxhttp.wrapper.entity.Progress

/**
 * 获取验证码
 * */
suspend fun userMobileCode(phone: String, type: Int): HttpResponseDataT<String> =
    RxHttp.postForm(USER_MOBILE_CODE)
        .add("phone", phone)
        .add("type", type)
        .toClass<HttpResponseDataT<String>>()
        .await()

/**
 * 获取用户信息
 * */
suspend fun userGetUserInfo(): HttpResponseDataT<UserInfo> =
    RxHttp.postForm(USER_GET_USER_INFO)
        .toClass<HttpResponseDataT<UserInfo>>()
        .await()

/**
 * 修改用户信息
 * */
suspend fun userChangeInfo(
    face: String? = null, address: String? = null,
    pet_name: String? = null, gender: String? = null,
    qq: String? = null, name: String? = null, idcard: String? = null,
): HttpResponseDataT<UserInfo> {
    val rxHttp = RxHttp.postForm(USER_CHANGE_USER_INFO)
        .addAll(httpSign(SPSConstance.uid, key = key1))
    if (!face.isNullOrEmpty())
        rxHttp.add("face", face)
    if (!address.isNullOrEmpty())
        rxHttp.add("address", address)
    if (!pet_name.isNullOrEmpty())
        rxHttp.add("pet_name", pet_name)
    if (!gender.isNullOrEmpty())
        rxHttp.add("gender", gender)
    if (!qq.isNullOrEmpty())
        rxHttp.add("qq", qq)
    if (!name.isNullOrEmpty())
        rxHttp.add("name", name)
    if (!idcard.isNullOrEmpty())
        rxHttp.add("idcard", idcard)
    return rxHttp
        .toClass<HttpResponseDataT<UserInfo>>().await()
}

/**
 * 获取我的页面信息
 * */
suspend fun userMyInfo(): HttpResponseDataT<UserMine> =
    RxHttp.postForm(USER_MY_INFO)
        .toClass<HttpResponseDataT<UserMine>>()
        .await()

/**
 * 我的代金券
 * */
suspend fun userMyCoupon(
    type: String, page: String, limit: String,
): HttpResponseDataE<WelfareCoupon> = RxHttp.postForm(USER_MY_COUPON)
    .add("type", type)
    .add("p", page)
    .add("limit", limit)
    .toClass<HttpResponseDataE<WelfareCoupon>>()
    .await()

/**
 * 代金券领取
 * */
suspend fun userAddCoupon(cid: String): HttpResponseDataT<WelfareCoupon> =
    RxHttp.postForm(USER_ADD_COUPON)
        .add("id", cid)
        .toClass<HttpResponseDataT<WelfareCoupon>>()
        .await()

suspend fun userDownload(
    urlPath: String, destPath: String,
    progress: (suspend (Progress) -> Unit)? = null,
): HttpResponseDataT<String> {
    val path = RxHttp.get(urlPath).toDownload(destPath, true, 5, progress)
        .await()
    return HttpResponseDataT(path).apply {
        this.code = 1
        this.message = "下载成功"
    }
}

